#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTimer>
#include <QSerialPort>

QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:

    void initSerialPort();
    void updateOpenButtonState();
    enum State {
        Stopped,
        Opened,
        Running
    };
    void setUiState(State state);
    bool openSerialPort();
    void closeSerialPort();
    void serialPortWriteCommand(QByteArray cmd);

private slots:

    void serialPortScan();
    void serialPortRead();

    void on_pushButton_Connect_clicked();

    void on_pushButton_CommTest_clicked();
    void on_pushButton_ClearFault_clicked();
    void on_pushButton_ClearCountInfo_clicked();
    void on_pushButton_EnterMode_clicked();
    void on_pushButton_ExitMode_clicked();
    void on_pushButton_StartCount_clicked();
    void on_pushButton_PauseCount_clicked();
    void on_pushButton_StartStore_clicked();
    void on_pushButton_CompleteStore_clicked();
    void on_pushButton_OpenShell_clicked();
    void on_pushButton_CloseShell_clicked();
    void on_pushButton_EnterCoinMode_clicked();
    void on_pushButton_ExitCoinMode_clicked();

    void on_pushButton_IntelligentMode_clicked();

    void on_pushButton_MixedMode_clicked();

    void on_pushButton_CountMode_clicked();

private:
    Ui::MainWindow *ui;

    QSerialPort *m_port;
    bool m_serial_port_closed;
    QTimer m_portscan_timer;
    QTimer m_portdata_timer;

    QByteArray m_data_received;
    QByteArray m_last_command;
};
#endif // MAINWINDOW_H
